<?php
/**
 * 员工账号管理
 *
 * @author maomao.thx@gmail.com
 * @copyright Copyright (c) 2009 Green Shore (http://www.iwgame.com)
 * @version $Id: EmployeeController.php 1059 2011-11-18 09:26:15Z mmxie $
 */
class System_EmployeeController extends IG_Controller_Action
{
    /**
     * 密码修改 & 密保管理
     * @author maomao.thx@gmail.com
     */
    public function myAction()
    {
        if ($this->getRequest()->isPost()) {
            $oldPassword = md5($this->_getParam('old_password'));
            $newPassword = md5($this->_getParam('new_password'));
            $employee = new System_Model_Employee();

            if ($this->getIdentity()->getPassword() == $oldPassword) {
                $this->getIdentity()->setPassword($newPassword)->save();
                $this->_helper->json(array(
                    'statusCode'   => 0,
                    'message'      => '操作成功',
                    'callbackType' => 'closeCurrent',
                ));
            } else {
                $this->_helper->json(array('statusCode' => 1, 'message' => '旧密码不正确'));
            }
        }
        //@todo 密保绑定, 更换, 校准
    }

    /**
     * 账号列表
     * @author maomao.thx@gmail.com
     */
    public function listAction ()
    {
        $department = new System_Model_Department();
        $departmentData = $department->fetchKeyPair(array('department_id', 'name'));
        $report = IG_Data::factory('array', array(
            'source'  => array(
                'name'   => 'Model',
                'config' => array(
                    'model' => 'System_Model_Employee',
                    'methodParams' => array(
                        array(
                            'department_id=?' => 'department_id',
                            'name=?'          => 'name',
                        ),
                    ),
                ),
            ),
            'plugins' => array(
                array(
                    'name'   => 'DwzTable',
                    'config' => array(
                        'columns' => array(
                            'employee_id'   => '用户ID',
                            'department_id' => array(
                                'title'     => '部门',
                                'valueMap'  => $departmentData
                            ),
                            'name'          => '账号',
                            'real_name'     => '真实姓名',
                            'email'         => 'Email',
                            'passpod'       => '密保序号',
                            'status'        => array(
                                'title' => '状态',
                                'valueMap' => array(
                                    1 => '开启',
                                    2 => '停用',
                                    3 => '禁用',
                                )
                            ),
                        ),
                        'attrs' => array('layoutH' => 137),
                    )
                ),
                array(
                    'name'   => 'Params',
                    'config' => array(
                        'department_id' => array(
                            'title'     => '部门',
                            'type'      => 'dwzSelect',
                            'params'    => array(
                                null,
                                array('' => '-请选择-') + $departmentData
                            ),
                            'default'   => '',
                        ),
                        'name'          => array(
                            'title' => '账号',
                            'type'  => 'formText',
                            'default'   => '',
                        ),
                    ),
                ),
                array(
                    'name'   => 'DwzPanelBar',
                    'config' => array(
                        'buttons' => array(
                            '添加' => 'class="add" href="/system/employee/save/" ' .
                                      'target="navTab" title="添加账号"',
                            '编辑' => 'class="edit" href="/system/employee/save/id/{employee_id}" ' .
                                      'target="navTab"' .
                                      'title="编辑账号"',
                            '删除' => 'class="delete" ' .
                                      'href="/system/employee/delete/id/{employee_id}" ' .
                                      'target="ajaxTodo" title="确定要删除吗?" ' .
                                      'callback="IG.data.delete"',
                            'line',
                            '角色分配' => 'class="icon" ' .
                                         'href="/system/employee/assign/id/{employee_id}" ' .
                                         'target="navTab"',

                        ),
                    ),
                ),
            ),
        ));

        $report->loadPlugins();
    }

    /**
     * 账号新增、修改操作
     * @author maomao.thx@gmail.com
     */
    public function saveAction ()
    {
        $employee = new System_Model_Employee();
        if ($this->getRequest()->isPost()) {
            $params = $this->_getParams(array(
                'department_id', 'email', 'employee_id', 'name', 'password',
                'real_name', 'status'
            ));
            if (empty($params['password'])) {
                unset($params['password']);
            } else {
                $params['password'] = md5($params['password']);
            }
            if (!empty($params['employee_id'])) {
                unset($params['name']);
            }

            try {
                $result = $employee->setOptions($params)
                                   ->save();
            } catch (Exception $e) {
                if ($e->getCode() == '23000') {
                    $this->_helper->json(array(
                        'statusCode' => 1,
                        'message'    => "账号已存在"
                    ));
                }
            }

            $this->_helper->json(array(
                'statusCode'   => (int) ($result === false),
                'message'      => '',
                'callbackType' => 'closeCurrent',
                'navTabId'     => '/system/employee/list'
            ));
        }

        if ($id = $this->_getParam('id')) {
            $this->view->assign('detail', $employee->find($id));
        } else {
            $this->view->assign('detail', $employee);
        }

        $department = new System_Model_Department();
        $this->view->assign('department', $department->getKeyPair());
    }

    /**
     * 账号删除
     * @author maomao.thx@gmail.com
     */
    public function deleteAction()
    {
        if ($this->getRequest()->isPost()) {
            $ids = (array) $this->_getParam('id');
            $employee = new System_Model_Employee();

            $result = 0;
            foreach ($ids as $id) {
                if (!$employee->delete($id)) {
                    $result = 1;
                    break;
                }
            }

            $this->_helper->json(array(
                'statusCode' => $result,
                'message'    => '操作成功!',
            ));
        }
        exit;
    }

    /**
     * 账号分配角色
     * @author maomao.thx@gmail.com
     */
    public function assignAction()
    {
        $employeeRole = new System_Model_EmployeeRole();
        if ($this->getRequest()->isPost()) {
            $employeeId    = $this->_getParam('id');
            $roles = $this->_getParam('role_id', array());
            $employeeRole->saveEmployeeRole($employeeId, $roles);

            Zend_Registry::get('lvan_cache')->remove('IG_Acl');
            $this->_helper->json(array( 'statusCode' => 0, 'message' => '操作成功！' ));
        }

        $this->setNoRender();
        $id     = $this->_getParam('id');
        $role   = new System_Model_Role();
        $report = IG_Data::factory('array', array(
            'source'  => array(
                'name'   => 'Model',
                'config' => array(
                    'model'  => 'System_Model_Role',
                    'method' => 'fetchAll'
                ),
            ),
            'plugins' => array(
                array(
                    'name'   => 'DwzTable',
                    'config' => array(
                        'columns' => array(
                            'role_id' => '角色ID',
                            'title'   => 'Title',
                            'name'    => '角色名',
                        ),
                        'attrs' => array('layoutH' => 137),
                        'checkbox' => array(
                            'name'    => 'role_id[]',
                            'key'     => 'role_id',
                            'checked' => array_keys($employeeRole->getRoleByEmployee($id))
                        ),
                    )
                ),
                array(
                    'name'   => 'DwzPanelBar',
                    'config' => array(
                        'buttons' => array(
                            '确认分配' => 'class="icon" target="selectedTodo" '.
                                         'href="/system/employee/assign/id/' . $id . '" ' .
                                         'rel="role_id[]"',
                        ),
                    ),
                ),
            ),
        ));
        $report->loadPlugins();
    }
}